package com.app.view.component.room.chips 
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.geom.Point;
	import flash.display.DisplayObject;
	
	import com.greensock.easing.Strong;
	import com.greensock.TweenLite;
	
	import com.peachcastle.core.Display;
	import com.peachcastle.tools.AutoWidthBack;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class ChipsView extends Display
	{
		// 筹码大小
		private const LIST:Array = [1, 2, 5, 10, 50, 100,
									500, 1000, 5000, 10000,
									50000, 100000, 500000, 1000000,
									5000000, 10000000];
								
		private const CHIP_THICK:Number = 1;						// 筹码厚度
		private const MAX_COUNT:int = 5;							// 筹码堆叠高度
		private const CHIP_RADIUS:Number = 8;						// 筹码半径	
		private const KEY_POINT_COUNT:int = 5;						// 关键点数目
		
		private const ALPHA_DURATION:Number = 0.3;					// 简便消失持续的时间
		
		private var _userChips:Array = null;
		private var _height:Number = 0;
		private var _currentChips:int = 0;
		
		private var _keyPoints:Array = null;
		private var _index:int  = 0;
		
		private var _chipsCount:TextField = null;
		
		private var _textBack:AutoWidthBack = null;	
		
		private var _frist:Point = null;
		
		public function ChipsView(ui:MovieClip) 
		{
			super(ui, true);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_chipsCount = assest.chipsCount;			
			_chipsCount.text = "";
			_chipsCount.selectable = false;

			_textBack = new AutoWidthBack(assest.textBack);
			_textBack.showBack(false);
			
			_frist = new Point(assest.x, assest.y);
			
			initKeyPoints();
		}
		
		/**
		 * 初始化关键点坐标
		 */
		private function initKeyPoints():void
		{
			_keyPoints = [new Point(0, 0)];
			var angle:Number = 0;
			for (var i:int = 0; i < KEY_POINT_COUNT; i++)
			{
				var point:Point = new Point();				
				point.x =  (2 * CHIP_RADIUS + CHIP_THICK) * Math.cos(angle);
				point.y = -(1 * CHIP_RADIUS + CHIP_THICK) * Math.sin(angle);
				_keyPoints.push(point);
				angle -= Math.PI / 3;
			}
		}
		
		/**
		 * 显示下注筹码
		 */
		public function showChips(chips:int):Boolean		
		{
			if (chips <= 0) //小于等于0 隐藏
			{
				resetChips();
				return false;
			}
			
			if (_currentChips > 0 && _currentChips == chips) //如果数目不变就不动
			{
				return true;
			}
			
			resetChips();
			
			showAssest();
			
			_currentChips = chips;

			_userChips = [];				

			if (_currentChips == 0)
			{
				_chipsCount.visible = false;
				_chipsCount.text = "";
				return false;
			}

			_chipsCount.visible = true;
			_chipsCount.text = _currentChips.toString();

			_textBack.adjustWidth(_chipsCount);
			
			var chipsnum:int = _currentChips;
			for (var i:int = LIST.length - 1; i >= 0; i--)			
			{
				var num:int = int(chipsnum / LIST[i]);
				if(num > 0)
				{
					var type:Array = [];
					type.push(num);
					type.push(LIST[i]);
					_userChips.push(type);
				}

				chipsnum %= LIST[i];							
			}
			_userChips = _userChips.filter(chipsFilter);			
			_userChips.sort(sortRule);
			
			for (i = 0; i < _userChips.length; i++)
			{
				addChipsToLayer(_userChips[i]);
			}
			assest.addChild(assest.textBack);
			assest.addChild(_chipsCount);

			return true;
		}	
		
		public function reset():void
		{
			resetChips();
			
			assest.x = _frist.x;
			assest.y = _frist.y;
		}
		
		/**
		 * 重置筹码
		 */
		private function resetChips():void
		{
			_currentChips = 0;
			assest.alpha = 1;
			
			clearChips();
		}
		
		/**
		 * 清空筹码
		 */
		private function clearChips():void
		{
			_height = 0;
			_index = 0;	
			
			while (assest.numChildren > 2)
			{
				var obj:DisplayObject = assest.getChildAt(0);
				if (obj == assest.chipsCount || obj == assest.textBack)
				{
					assest.addChild(obj);
					continue;
				}
				assest.removeChild(obj);
			}
			
			hideAssest();
		}
		
		/**
		 * 筹码类型
		 * @param	type
		 */
		private function addChipsToLayer(type:Array):void
		{
			var chipsClip:MovieClip = null;
			var point:Point = null;
			for (var i:int = 0; i < type[0]; i++)
			{				
				point = _keyPoints[_index];
				chipsClip = getChipsClip(type[1]);

				chipsClip.y = point.y - _height;
				chipsClip.x = point.x  + rangeRandom( -CHIP_THICK, CHIP_THICK);			
				assest.addChild(chipsClip);
				//chipsClip.alpha = 0;
				//TweenLite.to(chipsClip, i*0.2, {autoAlpha:1});
				_height += 2;
				
				// 计算筹码偏移
				var isAddNewPoint:Boolean = (_height / (3 * MAX_COUNT) >= 1 && _height > 0) ? true : false;
				if (isAddNewPoint == false)
					continue;				
				_index ++;
				_height = 0;
				if (_index >= KEY_POINT_COUNT)
					return;
			}
		}
		
		/**
		 * 自定义排序
		 * @param	data1
		 * @param	data2
		 * @param	list
		 * @return
		 */
		private function sortRule(data1:Array,data2:Array):int
		{
			if (data1[1] > data2[1])
			{
				return -1;
			}
			return 1;
		}
		
		/**
		 * 过滤出非零值
		 * @param	data
		 * @param	index
		 * @param	list
		 * @return
		 */
		private function chipsFilter(data:Array, index:int, list:Array):Boolean		
		{
			return (data[0] != 0) ? true : false;
		}
		
		/**
		 * 范围随机数
		 * @param	min
		 * @param	max
		 * @return
		 */
		private function rangeRandom(min:Number, max:Number):Number		
		{
			return Math.floor(Math.random() * (max - min + 1)) + min;			
		}
		
		/**
		 * 返回对应大小的筹码
		 * @param	value
		 * @return
		 */
		private function getChipsClip(value:int):MovieClip
		{
			var GameClass:Class = getNameByClass("room.Chips" + value) as Class;
			return (new GameClass()) as MovieClip;
		}	
	}

}